<?php
//touch("lock");
require_once("../shared/connect.php");

define('LOCK_TIMEOUT', 30);

// delete all locks older than LOCK_TIMEOUT seconds
function clear_old_locks() {
	$sql = "DELETE FROM `lock` WHERE last_modified < ".(time() - LOCK_TIMEOUT);
	mysql_query($sql) or die(mysql_error());
}

if ($_GET['name'] == "lock_van" && $_GET['van'] != "") { // lock_van

	clear_old_locks();

	$sql = "SELECT * FROM `lock` WHERE van = '$_GET[van]'";
	$query = mysql_query($sql) or die(mysql_error());

	if ($row = mysql_fetch_assoc($query)) { // van locked
		$sql = "DELETE FROM `lock` WHERE van = '$_GET[van]'";
		mysql_query($sql) or die(mysql_error());
	} else { // no lock?
		die("Warning: Tried to unlock when no lock_van was present.");
	}

} else if ($_GET['name'] == "lock_refresh" && $_GET['id'] != "") { // lock_refresh

	clear_old_locks();

	//$sql = "SELECT * FROM `lock` WHERE name = 'lock_refresh'";
	$sql = "DELETE FROM `lock` WHERE id = '$_GET[id]'";
	$query = mysql_query($sql) or die(mysql_error());
	/*
	if ($row = mysql_fetch_assoc($query)) { // lock_refresh already present; decrement it
		if ($row['count'] == 0) { // no lock?
			die("Warning: Tried to unlock when lock_refresh had count = 0. ");
		}

		$sql = "UPDATE `lock` SET count = count - 1 WHERE name = 'lock_refresh'";
		mysql_query($sql) or die(mysql_error());

	} else { // no lock?
		die("Warning: Tried to unlock when no lock_refresh was present. ");
	}*/

} else {
	die("Bad parameters? (unlock)");
}

?>